package com.heima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.heima.constant.MessageConstant;
import com.heima.dao.MemberDao;
import com.heima.dao.OrderDao;
import com.heima.dao.OrderSettingDao;
import com.heima.entity.Result;
import com.heima.pojo.Member;
import com.heima.pojo.Order;
import com.heima.pojo.OrderSetting;
import com.heima.service.OrderService;
import com.heima.utils.DateUtils;
import com.heima.vo.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Override
    public Result order(OrderInfo orderInfo) {
        //查看预约日期是否是可预约日期
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderInfo.getOrderDate());
        if (orderSetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //查看预约日期的预约人数是否已满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations>=number){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //查看用户是否重复预约(同一用户，同一天，同一个套餐)
        String telephone = orderInfo.getTelephone();
        Member member = memberDao.findByTelephone(telephone);
        if (member != null){
            Integer memberId = member.getId();
            Date orderDate = orderInfo.getOrderDate();
            Integer setmealId = orderInfo.getSetmealId();
            Order order = new Order(memberId,orderDate,setmealId);
            List<Order> list = orderDao.findByCondition(order);
            if (list!=null && list.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            //判断用户是否是会员，如果不是，就添加为会员
            member = new Member();
            member.setName(orderInfo.getName());
            member.setIdCard(orderInfo.getIdCard());
            member.setSex(orderInfo.getSex());
            member.setPhoneNumber(orderInfo.getTelephone());
            member.setSex(orderInfo.getSex());
            memberDao.add(member);
        }
        //添加会员成功，更新预约人数
        Order order = new Order();
        order.setSetmealId(orderInfo.getSetmealId());
        order.setMemberId(member.getId());
        order.setOrderDate(orderInfo.getOrderDate());
        order.setOrderType(orderInfo.getOrderType());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderDao.add(order);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map!=null){
           Date orderDate = (Date) map.get("orderDate");
           map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
